package com.cd.music.controller;

import com.cd.music.annotation.Auth;
import com.cd.music.annotation.Parameter;
import com.cd.music.dto.OrderDto;
import com.cd.music.dto.ResultBean;
import com.cd.music.service.OrderService;
import com.cd.music.utils.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: 订单控制器
 * @date 2018年03月05日  上午11:10
 * @address: 四川成都
 */
@Controller
@CrossOrigin
@RequestMapping("order")
public class OrderController {
    @Resource(name = "orderService")
    private OrderService orderService;

    /**
     * @author yxl 余秀良
     * @version 1.0
     * @description: 用途
     * @date 2018年03月05日 下午12:12
     * @address: 四川成都
     */
    @PostMapping("add")
    public String addOrder(@Parameter OrderDto dto, Model model) {
        ResultBean resultBean = orderService.insert(dto);
        boolean isSuccess = resultBean.getIsSuccess();
        String resultPage = "order_success";
        if (!isSuccess) {
            model.addAttribute("error", resultBean.getMessage());
            resultPage = "order/deal";
        }
        return resultPage;
    }

    @Auth(roles = "admin", sign = {0, 1}, description = "只有管理员和超级管理员可以访问")
    @ResponseBody
    @PostMapping("admin/update")
    public String adminUpdateOrder(@Parameter OrderDto dto) {
        return ResultUtil.result(orderService.update(dto));
    }

    @Auth(roles = "admin", sign = {0, 1}, description = "只有管理员和超级管理员可以访问")
    @ResponseBody
    @PostMapping("admin/list")
    public String adminSelectOrder(@Parameter OrderDto dto) {
        return ResultUtil.result(orderService.adminSelect(dto));
    }

    @RequestMapping("user/list")
    public String userSelectOrder(@Parameter OrderDto dto, Model model) {
        ResultBean resultBean = orderService.userSelect(dto);
        if (!resultBean.getIsSuccess()) {
            model.addAttribute("error", resultBean.getMessage());
        }
        model.addAttribute("order", resultBean.getData());
        return "order/order";
    }

}
